package com.bingxue.edu.management.generator.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.bingxue.edu.framework.common.model.PageQuery;
import com.bingxue.edu.framework.common.model.Pagination;
import com.bingxue.edu.framework.common.model.Response;
import com.bingxue.edu.management.generator.model.entity.FieldConfig;
import com.bingxue.edu.management.generator.model.entity.GenConfig;
import com.bingxue.edu.management.generator.model.query.TableQuery;
import com.bingxue.edu.management.generator.model.req.GenConfigReq;
import com.bingxue.edu.management.generator.model.resp.GeneratePreviewResp;
import com.bingxue.edu.management.generator.model.resp.TableResp;
import com.bingxue.edu.management.generator.service.GeneratorService;
import com.bingxue.edu.management.generator.service.TableMetaService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;
import java.util.List;

/**
 * 代码生成 API
 *
 * @author panbo
 * @since 2024/7/7
 */
@Tag(name = "代码生成 API")
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/generator")
public class GeneratorController {

    private final TableMetaService tableMetaService;
    private final GeneratorService generatorService;

    @Operation(summary = "分页查询数据表", description = "分页查询数据表")
    @SaCheckPermission("tool:generator:list")
    @GetMapping("/table")
    public Response<Pagination<TableResp>> pageTable(TableQuery query, @Validated PageQuery pageQuery) throws SQLException {
        return Response.success(tableMetaService.pageTable(query, pageQuery));
    }

    @Operation(summary = "查询字段配置列表", description = "查询字段配置列表")
    @Parameter(name = "tableName", description = "表名称", required = true, example = "sys_user", in = ParameterIn.PATH)
    @Parameter(name = "requireSync", description = "是否需要同步", example = "false", in = ParameterIn.QUERY)
    @SaCheckPermission("tool:generator:list")
    @GetMapping("/field/{tableName}")
    public Response<List<FieldConfig>> listFieldConfig(@PathVariable String tableName,
                                                       @RequestParam(required = false, defaultValue = "false") Boolean requireSync) {
        return Response.success(tableMetaService.listFieldConfig(tableName, requireSync));
    }

    @Operation(summary = "查询生成配置信息", description = "查询生成配置信息")
    @Parameter(name = "tableName", description = "表名称", required = true, example = "sys_user", in = ParameterIn.PATH)
    @SaCheckPermission("tool:generator:list")
    @GetMapping("/config/{tableName}")
    public Response<GenConfig> getGenConfig(@PathVariable String tableName) throws SQLException {
        return Response.success(tableMetaService.getGenConfig(tableName));
    }

    @Operation(summary = "保存配置信息", description = "保存配置信息")
    @Parameter(name = "tableName", description = "表名称", required = true, example = "sys_user", in = ParameterIn.PATH)
    @SaCheckPermission("tool:generator:list")
    @PostMapping("/config/{tableName}")
    public Response<Void> saveConfig(@Validated @RequestBody GenConfigReq req, @PathVariable String tableName) {
        tableMetaService.saveConfig(req, tableName);
        return Response.success();
    }

    @Operation(summary = "生成预览", description = "预览生成代码")
    @Parameter(name = "tableName", description = "表名称", required = true, example = "sys_user", in = ParameterIn.PATH)
    @SaCheckPermission("tool:generator:list")
    @GetMapping("/preview/{tableName}")
    public Response<List<GeneratePreviewResp>> preview(@PathVariable String tableName) {
        return Response.success(generatorService.preview(tableName));
    }

    @Operation(summary = "生成代码", description = "生成代码")
    @Parameter(name = "tableName", description = "表名称", required = true, example = "sys_user", in = ParameterIn.PATH)
    @SaCheckPermission("tool:generator:list")
    @PostMapping("/{tableNames}")
    public Response<Void> generate(@PathVariable List<String> tableNames) {
        generatorService.generate(tableNames);
        return Response.success();
    }

    @Operation(summary = "下载代码", description = "下载代码")
    @Parameter(name = "tableName", description = "表名称", required = true, example = "sys_user", in = ParameterIn.PATH)
    @SaCheckPermission("tool:generator:list")
    @PostMapping("/download/{tableNames}")
    public void download(@PathVariable List<String> tableNames,
                         HttpServletRequest request,
                         HttpServletResponse response) {
        generatorService.download(tableNames, request, response);
    }
}
